﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;

namespace iSun.AutoUploadToServer
{
    class Program
    {
        static void Main(string[] args)
        { 
             Upload();
             //Upload(true);
           
        }

        //static void Upload(bool bl)
        //{
        //    FtpClient ftp = new FtpClient("ftp:/127.0.0.1", "FTP", "huaiyu");
        //    ftp.Login();
        //    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("C:\\Files\\photo");
        //    IList<System.IO.FileInfo> _list = new List<System.IO.FileInfo>();
        //    GetFiles(dir, _list);
        //    foreach (System.IO.FileInfo file in _list)
        //    {
        //        ftp.Upload(file.FullName);
        //    }
        //}

        /// <summary>
        /// 上传文件
        /// </summary>
        static void Upload()
        {
            string FtpAddress = ConfigurationManager.AppSettings["ftpAddress"];
            string FtpUsername = ConfigurationManager.AppSettings["ftpUserName"];
            string FtpPassword = ConfigurationManager.AppSettings["ftpPassword"];
            string FtpFilePath = ConfigurationManager.AppSettings["ftpFilePath"];
            ftp ftp = new ftp(FtpAddress, FtpUsername,FtpPassword);

            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(FtpFilePath);
            IList<System.IO.FileInfo> _list = new List<System.IO.FileInfo>();
            GetFiles(dir, _list);
            foreach (System.IO.FileInfo file in _list)
            {
                //创建对应文件的服务器路径
                ftp.createDirectory("/etc/" + file.DirectoryName.Replace(FtpFilePath, ""));
                //上传文件到服务器对应的路径
                ftp.upload("etc/" + file.FullName.Replace(FtpFilePath, ""), file.FullName);
            }
        }

        static void GetFiles(System.IO.DirectoryInfo dir, IList<System.IO.FileInfo> list)
        {
            foreach(System.IO.FileInfo file in dir.GetFiles())
            {
                list.Add(file);
            }
            foreach (System.IO.DirectoryInfo _dir in dir.GetDirectories())
            {
                GetFiles(_dir, list);
            }
        }

         
    }
}
